package top.kaoshanji.p1ch01a;

import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import top.kaoshanji.p1ch01a.mapper.UserMapper;
import top.kaoshanji.p1ch01a.model.User;
import top.kaoshanji.p1ch01a.util.SqlSessionUtil;

import java.util.List;

/**
 * 测试 UserMapper
 * @author kaoshanji
 * ^_^
 * create time 2025/4/17 14:19
 */
public class UserMapperTest {

    /**
     * 代码清单test-p1ch01a-28页a：依据用户id查询
     */
    @Test
    public void testQueryUserById() {
        SqlSession sqlSession = null;
        try {
            sqlSession = SqlSessionUtil.getSqlSession();
            UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

            User user = userMapper.queryUserById(1);
            System.out.println(user);

        } finally {
            SqlSessionUtil.closeSqlSession(sqlSession);
        }
    }

    /**
     * 代码清单test-p1ch01a-29页a：查询所有用户
     */
    @Test
    public void testQueryAllUser() {
        SqlSession sqlSession = null;
        try {
            sqlSession = SqlSessionUtil.getSqlSession();
            UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

            List<User> userList = userMapper.queryAllUser();
            for (User user: userList) {
                System.out.println(user);
            }

        } finally {
            SqlSessionUtil.closeSqlSession(sqlSession);
        }
    }

    /**
     * 代码清单test-p1ch01a-30页a：插入用户
     */
    @Test
    public void testInsertUser() {
        SqlSession sqlSession = null;
        try {
            sqlSession = SqlSessionUtil.getSqlSession();
            UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

            User user = new User(null, "dodo", "333666", "female");
            int result = userMapper.insertUser(user);

            // 看下数据库...
            System.out.println("影响的行数："+result);

        } finally {
            SqlSessionUtil.closeSqlSession(sqlSession);
        }
    }

    /**
     * 代码清单test-p1ch01a-30页b：更新用户
     */
    @Test
    public void testUpdateUser() {
        SqlSession sqlSession = null;
        try {
            sqlSession = SqlSessionUtil.getSqlSession();
            UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

            // 看下数据库是不是有一条数据的 id 等于 5
            User user = new User(5, "dodo", "777777", "female");
            int result = userMapper.updateUser(user);

            // 看下数据库...
            System.out.println("影响的行数："+result);

        } finally {
            SqlSessionUtil.closeSqlSession(sqlSession);
        }
    }

    /**
     * 代码清单test-p1ch01a-31页a：删除用户
     */
    @Test
    public void testDeleteUserById() {
        SqlSession sqlSession = null;
        try {
            sqlSession = SqlSessionUtil.getSqlSession();
            UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

            // 看下数据库是不是有一条数据的 id 等于 5
            int result = userMapper.deleteUserById(5);

            // 看下数据库...
            System.out.println("影响的行数："+result);

        } finally {
            SqlSessionUtil.closeSqlSession(sqlSession);
        }
    }

}
